package com.hungryinlindholmen;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * Connects to the database and returns a ResultSet depending on what query is
 * made which in turn depends on a parameter from a radio button and ID of owner
 * currently logged in.
 * 
 * @author Ludvig Gee
 * 
 */

public class OwnerDb {
    Connection con;
    ResultSet rs;
    int table, option;
    String restId;

    public OwnerDb() {

    }

    /**
     * Creates database connection and queries the database.
     * 
     * @param radioButton
     *            radio button with value from either owner.jsp, ownerresult.jsp
     *            or edit.jsp
     * 
     * @param restID
     */

    public OwnerDb(String radioButton, String restID) {

	try {
	    Class.forName("com.mysql.jdbc.Driver");
	} catch (ClassNotFoundException e) {
	    System.out.println(e);
	}
	try {
	    con = DriverManager.getConnection(
		    "jdbc:mysql://db.student.chalmers.se/", "olofek",
		    "Ac6Jkdoa");
	} catch (Exception e) {
	    System.out.println(e);
	}

	if (radioButton.equals("a")) {
	    try {
		Statement stmt = con.createStatement();

		String query = "SELECT menu.dish, menu.description, menu.price, menu.type "
			+ "FROM olofek.menu AS menu "
			+ " WHERE menu.rest_id like '"
			+ restID
			+ "' "
			+ "ORDER BY menu.dish";
		table = 1;
		option = 1;
		restId = restID;
		rs = stmt.executeQuery(query);
	    } catch (Exception e) {
		System.out.println(e);
	    }
	}

	if (radioButton.equals("b")) {
	    try {
		Statement stmt = con.createStatement();

		String query = "SELECT ownerlogin.owner, ownerlogin.username, ownerlogin.password "
			+ "FROM olofek.ownerlogin AS ownerlogin "
			+ " WHERE ownerlogin.rest_id like '"
			+ restID
			+ "'"
			+ "ORDER BY ownerlogin.owner";
		option = 2;
		table = 2;
		rs = stmt.executeQuery(query);
	    } catch (Exception e) {
		System.out.println(e);
	    }
	}

	if (radioButton.equals("c")) {
	    try {
		Statement stmt = con.createStatement();

		String query = "SELECT lunch.date, lunch.dish, lunch.description, lunch.price, lunch.type "
			+ "FROM olofek.lunch AS lunch "
			+ " WHERE lunch.rest_id like '"
			+ restID
			+ "'"
			+ "ORDER BY lunch.date";
		table = 3;
		option = 3;

		rs = stmt.executeQuery(query);
	    } catch (Exception e) {
		System.out.println(e);
	    }
	}

	if (radioButton.equals("d")) {
	    try {
		Statement stmt = con.createStatement();

		String query = "SELECT restaurant.type, restaurant.adress, restaurant.description, restaurant.phone_nr "
			+ "FROM olofek.restaurant AS restaurant "
			+ " WHERE restaurant.rest_id like '" + restID + "'";
		option = 4;
		table = 4;
		rs = stmt.executeQuery(query);
	    } catch (Exception e) {
		System.out.println(e);
	    }
	}

	if (radioButton.equals("e")) {
	    try {
		Statement stmt = con.createStatement();

		String query = "SELECT restaurant.monday, restaurant.tuesday, restaurant.wednesday,"
			+ "restaurant.thursday, restaurant.friday, restaurant.saturday, restaurant.sunday "
			+ "FROM olofek.restaurant AS restaurant "
			+ " WHERE restaurant.rest_id like '" + restID + "'";
		option = 5;
		table = 5;
		rs = stmt.executeQuery(query);
	    } catch (Exception e) {
		System.out.println(e);
	    }
	}
    }

    public Connection getCon() {
	return con;
    }

    public void setCon(Connection con) {
	this.con = con;
    }

    public ResultSet getResultSet() {
	return rs;
    }

    /**
     * parameter which is used by edit.jsp to determine which row to display.
     * 
     * @return set depending on which query is made.
     */
    public int getTable() {
	return table;
    }

    /**
     * parameter which is used by edit.jsp to determine which row to display.
     * 
     * @return set depending on which query is made.
     */
    public int getOption() {
	return option;
    }

    public String getId() {
	return restId;
    }

}